package com.demo.chapter12;

public class VolatileTest {
    private static final int THREAD_COUNT = 20;

    public static volatile int race = 0;

    public static void increase() {
        race++;
        System.out.println(race);
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[THREAD_COUNT];
        for (int i = 0; i < THREAD_COUNT; ++i) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j< 10000; ++j) {
                        increase();
                    }
                }
            });
            threads[i].start();
        }
        for (Thread t : threads) {
            t.join();
        }
        System.out.println(race);
    }
}
